package qldc.pm.tienan.service.impl;

import java.io.File;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import qldc.pm.tienan.model.BienBanBanGiaoHoSo;
import qldc.pm.tienan.model.CongViecBanGiao;
import qldc.pm.tienan.model.DoiTuongTienAn;
import qldc.pm.tienan.model.KetQuaTrichLuc;
import qldc.pm.tienan.model.LichSuXoaAnTich;
import qldc.pm.tienan.model.PhieuThongTinVeNguoiCoTienAn;
import qldc.pm.tienan.model.TEMP_DoiTuongTienAn;
import qldc.pm.tienan.model.TEMP_PhieuThongTinVeNguoiCoTienAn;
import qldc.pm.tienan.model.TEMP_TienAn;
import qldc.pm.tienan.model.TangThu;
import qldc.pm.tienan.model.TienAn;
import qldc.pm.tienan.model.YeuCauTrichLuc;
import qldc.pm.tienan.service.BienBanBanGiaoHoSoLocalServiceUtil;
import qldc.pm.tienan.service.CongViecBanGiaoLocalServiceUtil;
import qldc.pm.tienan.service.DoiTuongTienAnLocalServiceUtil;
import qldc.pm.tienan.service.KetQuaTrichLucLocalServiceUtil;
import qldc.pm.tienan.service.LichSuXoaAnTichLocalServiceUtil;
import qldc.pm.tienan.service.PhieuThongTinVeNguoiCoTienAnLocalServiceUtil;
import qldc.pm.tienan.service.TEMP_DoiTuongTienAnLocalServiceUtil;
import qldc.pm.tienan.service.TEMP_PhieuThongTinVeNguoiCoTienAnLocalServiceUtil;
import qldc.pm.tienan.service.TEMP_TienAnLocalServiceUtil;
import qldc.pm.tienan.service.TangThuLocalServiceUtil;
import qldc.pm.tienan.service.TienAnLocalServiceUtil;
import qldc.pm.tienan.service.YeuCauTrichLucLocalServiceUtil;
import qldc.pm.tienan.service.base.PhucHoiTienAnLocalServiceBaseImpl;
import qldc.pm.tienan.service.persistence.TEMP_PhieuThongTinVeNguoiCoTienAnPersistence;
import qldc.pm.tienan.service.persistence.TEMP_TienAnPersistence;
import qldc.pm.tienan.service.persistence.TienAnPersistence;
import qldc.pm.tienan.service.persistence.YeuCauTrichLucPersistence;
import qldc.pm.tienan.utils.saoluudulieu.objects.BienBanBanGiaoHoSoJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.BienBanBanGiaoHoSoListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.CongViecBanGiaoJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.CongViecBanGiaoListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.DoiTuongTienAnJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.DoiTuongTienAnListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.KetQuaTrichLucJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.KetQuaTrichLucListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.LichSuXoaAnTichJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.LichSuXoaAnTichListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.PhieuThongTinVeNguoiCoTienAnJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.PhieuThongTinVeNguoiCoTienAnListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.TEMP_DoiTuongTienAnJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.TEMP_DoiTuongTienAnListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.TEMP_PhieuThongTinVeNguoiCoTienAnJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.TEMP_PhieuThongTinVeNguoiCoTienAnListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.TEMP_TienAnJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.TEMP_TienAnListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.TangThuJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.TangThuListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.TienAnJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.TienAnListJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.YeuCauTrichLucJAXB;
import qldc.pm.tienan.utils.saoluudulieu.objects.YeuCauTrichLucListJAXB;
import qldc.pm.tienan.utils.saoluuphuchoi.PhucHoiUtils;
import qldc.pm.tienan.utils.saoluuphuchoi.RootTienAnJAXB;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.Validator;

/**
 * The implementation of the phuc hoi tien an local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link qldc.pm.tienan.service.PhucHoiTienAnLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see qldc.pm.tienan.service.base.PhucHoiTienAnLocalServiceBaseImpl
 * @see qldc.pm.tienan.service.PhucHoiTienAnLocalServiceUtil
 */
public class PhucHoiTienAnLocalServiceImpl
    extends PhucHoiTienAnLocalServiceBaseImpl {

	  public void phucHoiDuLieu(long userId, File xmlFile) throws JAXBException, SystemException, IOException, PortalException{
		  System.out.println("File >>> "+xmlFile);
			if(null != xmlFile){
				//phuc hoi data
				byte[] xMLBytes = FileUtil.getBytes(xmlFile);
				if(null != xMLBytes){
					JAXBContext jaxbContext = JAXBContext.newInstance(RootTienAnJAXB.class);
					Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
					RootTienAnJAXB root = (RootTienAnJAXB) jaxbUnmarshaller.unmarshal(xmlFile);
					
					if(null != root){
						//phuc hoi data
						phucHoiBienBanBanGiaoHoSo(root.getBienBanBanGiaoHoSoList());
						phucHoiCongViecBanGiao(root.getCongViecBanGiaoList());
						phucHoiDoiTuongTienAn(root.getDoiTuongTienAnList());
						phucHoiKetQuaTrichLuc(root.getKetQuaTrichLucList());
						phucHoiLichSuXoaAnTich(root.getLichSuXoaAnTichList());
						phucHoiPhieuThongTinVeNguoiCoTienAn(root.getPhieuThongTinVeNguoiCoTienAnList());
						phucHoiTangThu(root.getTangThuList());
						phucHoiTEMP_DoiTuongTienAn(root.getTEMP_DoiTuongTienAnList());
						phucHoiTEMP_PhieuThongTinVeNguoiCoTienAn(root.getTEMP_PhieuThongTinVeNguoiCoTienAnList());
						phucHoiYeuCauTrichLuc(root.getYeuCauTrichLucList());
						phucHoiTEMP_TienAn(root.getTEMP_TienAnList());
						phucHoiTienAn(root.getTienAnList());
					}
					}
			
			} else {
				System.out.println("File is empty");
			}
	   }
	  private void phucHoiBienBanBanGiaoHoSo(BienBanBanGiaoHoSoListJAXB BienBanBanGiaoHoSoList) throws SystemException {
			bienBanBanGiaoHoSoPersistence.removeAll();
			//add new
			if (Validator.isNull(BienBanBanGiaoHoSoList) || Validator.isNull(BienBanBanGiaoHoSoList.getBienBanBanGiaoHoSo()))return;
			for (int i= 0;i< BienBanBanGiaoHoSoList.getBienBanBanGiaoHoSo().size();i++){
				BienBanBanGiaoHoSoJAXB jaxb = BienBanBanGiaoHoSoList.getBienBanBanGiaoHoSo().get(i);
				BienBanBanGiaoHoSo obj = bienBanBanGiaoHoSoPersistence.create(jaxb.getBienBanBanGiaoHoSoId());
				obj = PhucHoiUtils.convertFormBienBanBanGiaoHoSoToJAXB(jaxb, obj);
				BienBanBanGiaoHoSoLocalServiceUtil.addBienBanBanGiaoHoSo(obj);

			}
		}


		private void phucHoiCongViecBanGiao(CongViecBanGiaoListJAXB CongViecBanGiaoList) throws SystemException {
			congViecBanGiaoPersistence.removeAll();
			//add new
			if (Validator.isNull(CongViecBanGiaoList) || Validator.isNull(CongViecBanGiaoList.getCongViecBanGiao()))return;
			for (int i= 0;i< CongViecBanGiaoList.getCongViecBanGiao().size();i++){
				CongViecBanGiaoJAXB jaxb = CongViecBanGiaoList.getCongViecBanGiao().get(i);
				CongViecBanGiao obj = congViecBanGiaoPersistence.create(jaxb.getCongViecBanGiaoId());
				obj = PhucHoiUtils.convertFormCongViecBanGiaoToJAXB(jaxb, obj);
				CongViecBanGiaoLocalServiceUtil.addCongViecBanGiao(obj);

			}
		}
		private void phucHoiDoiTuongTienAn(DoiTuongTienAnListJAXB DoiTuongTienAnList) throws SystemException {
			doiTuongTienAnPersistence.removeAll();
			//add new
			if (Validator.isNull(DoiTuongTienAnList) || Validator.isNull(DoiTuongTienAnList.getDoiTuongTienAn()))return;
			for (int i= 0;i< DoiTuongTienAnList.getDoiTuongTienAn().size();i++){
				DoiTuongTienAnJAXB jaxb = DoiTuongTienAnList.getDoiTuongTienAn().get(i);
				DoiTuongTienAn obj =doiTuongTienAnPersistence.create(jaxb.getDoiTuongTienAnId());
				obj = PhucHoiUtils.convertFormDoiTuongTienAnToJAXB(jaxb, obj);
				DoiTuongTienAnLocalServiceUtil.addDoiTuongTienAn(obj);

			}
		}
		private void phucHoiKetQuaTrichLuc(KetQuaTrichLucListJAXB KetQuaTrichLucList) throws SystemException {
			ketQuaTrichLucPersistence.removeAll();
			//add new
			if (Validator.isNull(KetQuaTrichLucList) || Validator.isNull(KetQuaTrichLucList.getKetQuaTrichLuc()))return;
			for (int i= 0;i< KetQuaTrichLucList.getKetQuaTrichLuc().size();i++){
				KetQuaTrichLucJAXB jaxb = KetQuaTrichLucList.getKetQuaTrichLuc().get(i);
				KetQuaTrichLuc obj = ketQuaTrichLucPersistence.create(jaxb.getKetQuaTrichLucId());
				obj = PhucHoiUtils.convertFormKetQuaTrichLucToJAXB(jaxb, obj);
				KetQuaTrichLucLocalServiceUtil.addKetQuaTrichLuc(obj);

			}
		}
		private void phucHoiLichSuXoaAnTich(LichSuXoaAnTichListJAXB LichSuXoaAnTichList) throws SystemException {
			lichSuXoaAnTichPersistence.removeAll();
			//add new
			if (Validator.isNull(LichSuXoaAnTichList) || Validator.isNull(LichSuXoaAnTichList.getLichSuXoaAnTich()))return;
			for (int i= 0;i< LichSuXoaAnTichList.getLichSuXoaAnTich().size();i++){
				LichSuXoaAnTichJAXB jaxb = LichSuXoaAnTichList.getLichSuXoaAnTich().get(i);
				LichSuXoaAnTich obj = lichSuXoaAnTichPersistence.create(jaxb.getLichSuXoaAnTichId());
				obj = PhucHoiUtils.convertFormLichSuXoaAnTichToJAXB(jaxb, obj);
				LichSuXoaAnTichLocalServiceUtil.addLichSuXoaAnTich(obj);

			}
		}
		private void phucHoiPhieuThongTinVeNguoiCoTienAn(PhieuThongTinVeNguoiCoTienAnListJAXB PhieuThongTinVeNguoiCoTienAnList) throws SystemException {
			phieuThongTinVeNguoiCoTienAnPersistence.removeAll();
			//add new
			if (Validator.isNull(PhieuThongTinVeNguoiCoTienAnList) || Validator.isNull(PhieuThongTinVeNguoiCoTienAnList.getPhieuThongTinVeNguoiCoTienAn()))return;
			for (int i= 0;i< PhieuThongTinVeNguoiCoTienAnList.getPhieuThongTinVeNguoiCoTienAn().size();i++){
				PhieuThongTinVeNguoiCoTienAnJAXB jaxb = PhieuThongTinVeNguoiCoTienAnList.getPhieuThongTinVeNguoiCoTienAn().get(i);
				PhieuThongTinVeNguoiCoTienAn obj = phieuThongTinVeNguoiCoTienAnPersistence.create(jaxb.getPhieuThongTinVeNguoiCoTienAnId());
				obj = PhucHoiUtils.convertFormPhieuThongTinVeNguoiCoTienAnToJAXB(jaxb, obj);
				PhieuThongTinVeNguoiCoTienAnLocalServiceUtil.addPhieuThongTinVeNguoiCoTienAn(obj);

			}
		}
		private void phucHoiTangThu(TangThuListJAXB TangThuList) throws SystemException {
			tangThuPersistence.removeAll();
			//add new
			if (Validator.isNull(TangThuList) || Validator.isNull(TangThuList.getTangThu()))return;
			for (int i= 0;i< TangThuList.getTangThu().size();i++){
				TangThuJAXB jaxb = TangThuList.getTangThu().get(i);
				TangThu obj = tangThuPersistence.create(jaxb.getTangThuId());
				obj = PhucHoiUtils.convertFormTangThuToJAXB(jaxb, obj);
				TangThuLocalServiceUtil.addTangThu(obj);

			}
		}
		private void phucHoiTEMP_DoiTuongTienAn(TEMP_DoiTuongTienAnListJAXB TEMP_DoiTuongTienAnList) throws SystemException {
			temp_DoiTuongTienAnPersistence.removeAll();
			//add new
			if (Validator.isNull(TEMP_DoiTuongTienAnList) || Validator.isNull(TEMP_DoiTuongTienAnList.getTEMP_DoiTuongTienAn()))return;
			for (int i= 0;i< TEMP_DoiTuongTienAnList.getTEMP_DoiTuongTienAn().size();i++){
				TEMP_DoiTuongTienAnJAXB jaxb = TEMP_DoiTuongTienAnList.getTEMP_DoiTuongTienAn().get(i);
				TEMP_DoiTuongTienAn obj = temp_DoiTuongTienAnPersistence.create(jaxb.getDoiTuongTienAnId());
				obj = PhucHoiUtils.convertFormTEMP_DoiTuongTienAnToJAXB(jaxb, obj);
				TEMP_DoiTuongTienAnLocalServiceUtil.addTEMP_DoiTuongTienAn(obj);

			}
		}
		private void phucHoiTEMP_PhieuThongTinVeNguoiCoTienAn(TEMP_PhieuThongTinVeNguoiCoTienAnListJAXB TEMP_PhieuThongTinVeNguoiCoTienAnList) throws SystemException {
			temp_PhieuThongTinVeNguoiCoTienAnPersistence.removeAll();
			//add new
			if (Validator.isNull(TEMP_PhieuThongTinVeNguoiCoTienAnList) || Validator.isNull(TEMP_PhieuThongTinVeNguoiCoTienAnList.getTEMP_PhieuThongTinVeNguoiCoTienAn()))return;
			for (int i= 0;i< TEMP_PhieuThongTinVeNguoiCoTienAnList.getTEMP_PhieuThongTinVeNguoiCoTienAn().size();i++){
				TEMP_PhieuThongTinVeNguoiCoTienAnJAXB jaxb = TEMP_PhieuThongTinVeNguoiCoTienAnList.getTEMP_PhieuThongTinVeNguoiCoTienAn().get(i);
				TEMP_PhieuThongTinVeNguoiCoTienAn obj = temp_PhieuThongTinVeNguoiCoTienAnPersistence.create(jaxb.getPhieuThongTinVeNguoiCoTienAnId());
				obj = PhucHoiUtils.convertFormTEMP_PhieuThongTinVeNguoiCoTienAnToJAXB(jaxb, obj);
				TEMP_PhieuThongTinVeNguoiCoTienAnLocalServiceUtil.addTEMP_PhieuThongTinVeNguoiCoTienAn(obj);

			}
		}

		private void phucHoiTEMP_TienAn(TEMP_TienAnListJAXB TEMP_TienAnList) throws SystemException {
			temp_TienAnPersistence.removeAll();
			//add new
			if (Validator.isNull(TEMP_TienAnList) || Validator.isNull(TEMP_TienAnList.getTEMP_TienAn()))return;
			for (int i= 0;i< TEMP_TienAnList.getTEMP_TienAn().size();i++){
				TEMP_TienAnJAXB jaxb = TEMP_TienAnList.getTEMP_TienAn().get(i);
				TEMP_TienAn obj = temp_TienAnPersistence.create(jaxb.getTienAnId());
				obj = PhucHoiUtils.convertFormTEMP_TienAnToJAXB(jaxb, obj);
				TEMP_TienAnLocalServiceUtil.addTEMP_TienAn(obj);

			}
		}

		private void phucHoiTienAn(TienAnListJAXB TienAnList) throws SystemException {
			tienAnPersistence.removeAll();
			//add new
			if (Validator.isNull(TienAnList) || Validator.isNull(TienAnList.getTienAn()))return;
			for (int i= 0;i< TienAnList.getTienAn().size();i++){
				TienAnJAXB jaxb = TienAnList.getTienAn().get(i);
				TienAn obj = tienAnPersistence.create(jaxb.getTienAnId());
				obj = PhucHoiUtils.convertFormTienAnToJAXB(jaxb, obj);
				TienAnLocalServiceUtil.addTienAn(obj);

			}
		}

		private void phucHoiYeuCauTrichLuc(YeuCauTrichLucListJAXB YeuCauTrichLucList) throws SystemException {
			yeuCauTrichLucPersistence.removeAll();
			//add new
			if (Validator.isNull(YeuCauTrichLucList) || Validator.isNull(YeuCauTrichLucList.getYeuCauTrichLuc()))return;
			for (int i= 0;i< YeuCauTrichLucList.getYeuCauTrichLuc().size();i++){
				YeuCauTrichLucJAXB jaxb = YeuCauTrichLucList.getYeuCauTrichLuc().get(i);
				YeuCauTrichLuc obj = yeuCauTrichLucPersistence.create(jaxb.getYeuCauTrichLucId());
				obj = PhucHoiUtils.convertFormYeuCauTrichLucToJAXB(jaxb, obj);
				YeuCauTrichLucLocalServiceUtil.addYeuCauTrichLuc(obj);

			}
		}
}
